# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os

from django.conf.urls import patterns, include, url
from django.contrib import admin
from wenlincms.core.views import direct_to_template

from mainsys.settings import APP_ROOT, APPS_CANNOT_INTSALL

admin.autodiscover()

# 管理后台地址
urlpatterns = patterns("", url("^admin/", include(admin.site.urls)), )

# 根据wlapps里的目录进行动态添加url
for ap in os.listdir(APP_ROOT):
    if ap not in APPS_CANNOT_INTSALL and os.path.isdir(os.path.join(APP_ROOT, ap)) and \
            os.path.exists(os.path.join(os.path.join(APP_ROOT, ap), "__init__.py")) and \
            os.path.exists(os.path.join(os.path.join(APP_ROOT, ap), "urls.py")):
        urlpatterns += patterns("", ("^", include(ap + ".urls")), )

# 其它地址
urlpatterns += patterns("",
                        url("^$", direct_to_template, {"template": "index.html"}, name="home"),
                        ("^", include("wenlincms.urls")),
                        )

# 自定义404和500错误页面
handler404 = "wlapps.wlcore.views.page_not_found"
handler500 = "wlapps.wlcore.views.server_error"
handler403 = "wlapps.wlcore.views.forbidden"
handler400 = "wlapps.wlcore.views.bad_request"
